//
//  MainTabBarController.m
//  MyMovie
//
//  Created by zsm on 14-8-15.
//  Copyright (c) 2014年 zsm. All rights reserved.
//

#import "MainTabBarController.h"
#import "HomeVC.h"
#import "BaseNavigationController.h"
#import "HotVC.h"
#import "ApiVC.h"
#import "HomeOneVC.h"
#import "MontiorHomeVC.h"
#import "LiveVideoVC.h"


@interface MainTabBarController ()<AxcAE_TabBarDelegate>
@property (nonatomic, strong) CAEmitterLayer * rainLayer;
@property (nonatomic, strong) RainBackView *rainView;


@end

static MainTabBarController *mainTVC = nil;


@implementation MainTabBarController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)setupEmitter{
    
    // 停止下雨
//    [self.rainLayer setValue:@0.f forKeyPath:@"birthRate"];
//    // 开始下雨
//    [self.rainLayer setValue:@1.f forKeyPath:@"birthRate"];
//    [self.rainLayer setValue:@(self.rainLayer.birthRate + rate) forKeyPath:@"birthRate"];
//    [self.rainLayer setValue:@(self.rainLayer.scale + scale) forKeyPath:@"scale"];
    
    // 1. 设置CAEmitterLayer
    CAEmitterLayer * rainLayer = [CAEmitterLayer layer];
    // 2.在背景图上添加粒子图层
    [self.rainView.layer addSublayer:rainLayer];
    self.rainLayer = rainLayer;
    
    //3.发射形状--线性
    rainLayer.emitterShape = kCAEmitterLayerLine;
    //发射模式
    rainLayer.emitterMode = kCAEmitterLayerSurface;
    //发射源大小
    rainLayer.emitterSize = self.view.frame.size;
    //发射源位置 y最好不要设置为0 最好<0
    rainLayer.emitterPosition = CGPointMake(self.view.bounds.size.width * 0.5, -10);
    
    // 2. 配置cell
    CAEmitterCell * snowCell = [CAEmitterCell emitterCell];
    //粒子内容
    snowCell.contents = (id)[[UIImage imageNamed:@"shuidi"] CGImage];
    //每秒产生的粒子数量的系数
    snowCell.birthRate = 25.f;
    //粒子的生命周期
    snowCell.lifetime = 20.f;
    //speed粒子速度.图层的速率。用于将父时间缩放为本地时间，例如，如果速率是2，则本地时间的进度是父时间的两倍。默认值为1。
    snowCell.speed = 10.f;
    //粒子速度系数, 默认1.0
    snowCell.velocity = 10.f;
    //每个发射物体的初始平均范围,默认等于0
    snowCell.velocityRange = 10.f;
    //粒子在y方向的加速的
    snowCell.yAcceleration = 1000.f;
    //粒子缩放比例: scale
    snowCell.scale = 0.5;
    //粒子缩放比例范围:scaleRange
    snowCell.scaleRange = 0.f;
    
    // 3.添加到图层上
    rainLayer.emitterCells = @[snowCell];
    [self.rainLayer setValue:@0.f forKeyPath:@"birthRate"];
//    [self.rainLayer setValue:@1.f forKeyPath:@"birthRate"];

}




- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addChildViewControllers];
    mainTVC = self;
    if (@available(iOS 13.0, *)) {
        [self setOverrideUserInterfaceStyle:UIUserInterfaceStyleLight];
    } else {
        // Fallback on earlier versions
    }if (@available(iOS 12.0, *)) {
        if (@available(iOS 13.0, *)) {
            [self setOverrideUserInterfaceStyle:UIUserInterfaceStyleLight];
        } else {
            // Fallback on earlier versions
        }
    } else {
        // Fallback on earlier versions
    }
    
    self.rainView = [[RainBackView alloc] init];
    self.rainView.frame = CGRectMake(0, 0, HWScreenW, HWScreenH);
    self.rainView.backgroundColor = [UIColor clearColor];
//    [self.view addSubview:self.rainView];
    
    [self setupEmitter];
    
    [self xw_addNotificationForName:@"weather" block:^(NSNotification * _Nonnull notification) {
        NSDictionary *info = notification.userInfo;
        NSString *weatherStr = [NSString stringWithFormat:@"%@",info[@"weather"]];
        
        int score = [HandTool scoreWithweather:weatherStr];
        if (score <= 2) {
            float birthRate = 1 - 0.3 * score;
            [self.rainLayer setValue:@(birthRate) forKeyPath:@"birthRate"];

        }else{
            [self.rainLayer setValue:@0.f forKeyPath:@"birthRate"];

        }

    }];
    
}


- (void)addChildViewControllers{
    // 创建选项卡的数据 想怎么写看自己，这块我就写笨点了
    
    HomeVC *vc1 = [HomeVC new];
    vc1.hidesBottomBarWhenPushed = NO;
    BaseNavigationController *nav1 = [[BaseNavigationController alloc] initWithRootViewController:vc1];
    
    HotVC *vc2 = [HotVC new];
    vc2.hidesBottomBarWhenPushed = NO;
    BaseNavigationController *nav2 = [[BaseNavigationController alloc] initWithRootViewController:vc2];
    
    ApiVC *apiVC = [ApiVC new];
    apiVC.hidesBottomBarWhenPushed = NO;
    BaseNavigationController *nav3 = [[BaseNavigationController alloc] initWithRootViewController:apiVC];
    
   
    
    MontiorHomeVC *mvc = [MontiorHomeVC new];
    mvc.hidesBottomBarWhenPushed = NO;
    BaseNavigationController *nav4 = [[BaseNavigationController alloc] initWithRootViewController:mvc];
    
    
//    FindCostVC *vc5 = [FindCostVC new];
//    vc5.hidesBottomBarWhenPushed = NO;
//    BaseNavigationController *nav5 = [[BaseNavigationController alloc] initWithRootViewController:vc5];
    
//    PositionHomeVC *vc5 = [PositionHomeVC new];
//    vc5.hidesBottomBarWhenPushed = NO;
//    BaseNavigationController *nav5 = [[BaseNavigationController alloc] initWithRootViewController:vc5];
    
    LiveVideoVC *vc5 = [LiveVideoVC new];
    vc5.hidesBottomBarWhenPushed = NO;
    BaseNavigationController *nav5 = [[BaseNavigationController alloc] initWithRootViewController:vc5];
    
    
    



    NSArray <NSDictionary *>*VCArray;
    
    
    VCArray = @[
        @{@"vc":nav1,@"itemTitle":@"热点",@"selectImg":@"remenredian-2",@"normalImg":@"remenredian"},
        @{@"vc":nav2,@"itemTitle":@"屠龙刀",@"selectImg":@"dao-4",@"normalImg":@"dao-3"},
        @{@"vc":nav4,@"itemTitle":@"监控",@"selectImg":@"jiankongzhibiao-2",@"normalImg":@"jiankongzhibiao"},
        @{@"vc":nav5,@"itemTitle":@"视频",@"selectImg":@"shipin-6",@"normalImg":@"shipin-5"},
        @{@"vc":nav3,@"itemTitle":@"API",@"selectImg":@"URLguanli-2",@"normalImg":@"URLguanli-3"},

        
        
        ];
        
    // 1.遍历这个集合
    // 1.1 设置一个保存构造器的数组
    NSMutableArray *tabBarConfs = @[].mutableCopy;
    // 1.2 设置一个保存VC的数组
    NSMutableArray *tabBarVCs = @[].mutableCopy;
    [VCArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        // 2.根据集合来创建TabBar构造器
        AxcAE_TabBarConfigModel *model = [AxcAE_TabBarConfigModel new];
        // 3.item基础数据三连
        model.itemLayoutStyle = AxcAE_TabBarItemLayoutStyleTopPictureBottomTitle;
        model.pictureWordsMargin = -2;
        model.titleLabel.font = [UIFont systemFontOfSize:10];
        model.itemTitle = [obj objectForKey:@"itemTitle"];
        model.selectImageName = [obj objectForKey:@"selectImg"];
        model.normalImageName = [obj objectForKey:@"normalImg"];
        // 4.设置单个选中item标题状态下的颜色
        model.selectColor = [HandTool getThemeColorWithWeather:[AppService shareInstance].weather];
        model.selectTintColor = [HandTool getThemeColorWithWeather:[AppService shareInstance].weather];

        model.normalColor = Color_gray;
        if (idx == 6) {
            model.bulgeStyle = AxcAE_TabBarConfigBulgeStyleSquare;
            model.bulgeHeight = 25;
            model.componentMargin = UIEdgeInsetsMake(0, 0, 0, 0 );
            model.icomImgViewSize = CGSizeMake(50, 50);
            model.pictureWordsMargin = 20;
            model.backgroundImageView.hidden = YES;
            model.selectColor = [UIColor whiteColor];
            model.normalColor = [UIColor whiteColor];
            model.titleLabel.backgroundColor = [UIColor colorWithHexString:@"#FF6C9D"];
            model.titleLabel.layer.cornerRadius = 7;
            model.titleLabel.layer.masksToBounds = YES;
            model.titleLabelSize = CGSizeMake(68, 6);
            
        }else{
            model.bulgeStyle = AxcAE_TabBarConfigBulgeStyleNormal;
            
        }
        
        // 备注 如果一步设置的VC的背景颜色，VC就会提前绘制驻留，优化这方面的话最好不要这么写
        // 示例中为了方便就在这写了
        BaseNavigationController *nav = [obj objectForKey:@"vc"];
        // 5.将VC添加到系统控制组
        [tabBarVCs addObject:nav];
        
        // 5.1添加构造Model到集合
        [tabBarConfs addObject:model];
    }];
    // 5.2 设置VCs -----
    // 一定要先设置这一步，然后再进行后边的顺序，因为系统只有在setViewControllers函数后才不会再次创建UIBarButtonItem，以免造成遮挡
    // 大意就是一定要让自定义TabBar遮挡住系统的TabBar
    self.viewControllers = tabBarVCs;
    //////////////////////////////////////////////////////////////////////////
    // 注：这里方便阅读就将AE_TabBar放在这里实例化了 使用懒加载也行
    // 6.将自定义的覆盖到原来的tabBar上面
    // 这里有两种实例化方案：
    // 6.1 使用重载构造函数方式：
    //    self.axcTabBar = [[AxcAE_TabBar alloc] initWithTabBarConfig:tabBarConfs];
    // 6.2 使用Set方式：
    self.axcTabBar = [AxcAE_TabBar new] ;
    self.axcTabBar.tabBarConfig = tabBarConfs;
    
    // 7.设置委托
    self.axcTabBar.delegate = self;
    // 8.添加覆盖到上边
    [self.tabBar addSubview:self.axcTabBar];
    
    //    [self.tabBar setShadowImage:[UIImage imageWithColor:[MyColor colorWithHexString:@"#F1F1F8"]]];
    [self.tabBar setShadowImage:[UIImage new]];
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    self.tabBar.backgroundColor = [UIColor whiteColor];
    
    
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -0.5, kScreenWidth, 1)];
//        view.backgroundColor = [UIColor colorWithHexString:@"#F1F1F8"];
//    [[UITabBar appearance] insertSubview:view atIndex:0];
    [self addLayoutTabBar]; // 10.添加适配
}
// 9.实现代理，如下：
- (void)axcAE_TabBar:(AxcAE_TabBar *)tabbar selectIndex:(NSInteger)index{
    [self setSelectedIndex:index];
    _lastIdx = index;
}


- (void)setSelectedIndex:(NSUInteger)selectedIndex{
    [super setSelectedIndex:selectedIndex];
    if(self.axcTabBar){
        self.axcTabBar.selectIndex = selectedIndex;
    }
}

// 10.添加适配
- (void)addLayoutTabBar{
    // 使用重载viewDidLayoutSubviews实时计算坐标 （下边的 -viewDidLayoutSubviews 函数）
    // 能兼容转屏时的自动布局
}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.axcTabBar.frame = self.tabBar.bounds;
    [self.axcTabBar viewDidLayoutItems];
}
- (BOOL)shouldAutorotate {
    return [self.selectedViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}

//单例方法
+ (instancetype)shareMainTabBarController
{
    return mainTVC;
    
}

+ (void)clearMainTabBarVC
{
    mainTVC = nil;
}

@end
